# -*- codeing = Utf-8 -*-
# @Time : 2022/9/6 10:00
# @Autgor : 影驰
# @File： housePrice.py
# @Software : PyCharm
import os

import requests
import asyncio
from lxml import etree
import aiofiles
import aiohttp
from bs4 import BeautifulSoup

top_url = "https://fangjia.gotohui.com/top"
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
}
top_url_search_areas_names = "/html/body/div[4]/div/div[1]/div/div/ul/li/a/text()"
top_url_search_areas_urls = "/html/body/div[4]/div/div[1]/div/div/ul/li/a/@href"
next_url_search_areas_trs = "/html/body/div[4]/div/div[2]/div[2]/table/tbody/tr"


async def area_house_prices(area_name, area_url):
    with open("RegionalHousePrice/全部地区平均.csv", mode="a") as fall:
        with open(f"RegionalHousePrice/{area_name}.csv", mode="a") as f:
            async with aiohttp.ClientSession() as session:
                async with session.get(area_url, headers=headers) as resp:
                    next_html = await resp.text()
                    nex_html_page = BeautifulSoup(next_html, "html.parser")
                    next_search_search_areas_trs = nex_html_page.find_all("tr")
                    prices = 0
                    for tr in next_search_search_areas_trs:
                        index = 0
                        lst = tr.find_all("td")
                        if len(lst) != 0:
                            for td in lst:
                                tdtext = td.text
                                for i in tdtext:
                                    if i == ',':
                                        tdtext = tdtext.replace(i, '')
                                if (index == 2):
                                    prices += int(tdtext)
                                index += 1
                                f.write(tdtext)
                                f.write(",")
                            f.write("\n")
                    prices = round(prices / (len(next_search_search_areas_trs)-1),2)
                    fall.write(area_name)
                    fall.write(",")
                    fall.write(str(prices))
                    fall.write("\n")
    print(area_name,"over!")

def del_files(dir_path):
    if os.path.isfile(dir_path):
        try:
            os.remove(dir_path)
        except BaseException as e:
            print(e)
    elif os.path.isdir(dir_path):
        file_lis = os.listdir(dir_path)
        for file_name in file_lis:
            tf = os.path.join(dir_path, file_name)
            del_files(tf)


async def main(top_url):
    del_files("RegionalHousePrice")
    top_html_resp = requests.get(url=top_url, headers=headers)
    top_html_resp.close()
    top_html_tree = etree.HTML(top_html_resp.text)  # 获取到地区
    top_html_areas_names = top_html_tree.xpath(top_url_search_areas_names)
    top_html_areas_urls = top_html_tree.xpath(top_url_search_areas_urls)
    index = 0
    tasks = []
    for area in top_html_areas_names:
        tasks.append(asyncio.create_task(area_house_prices(area + "", top_html_areas_urls[index] + "")))
        index += 1
    await asyncio.wait(tasks)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(top_url))

    # asyncio.run(main(top_url))
